package com.wolfalpha.utron.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.wolfalpha.utron.R;
import com.wolfalpha.utron.activity.base.BaseNormalActivity;
import com.wolfalpha.utron.adapter.ItemProjectAdapter;
import com.wolfalpha.utron.adapter.ItemProjectAttentionedAdapter;
import com.wolfalpha.utron.app.AppConfig;
import com.wolfalpha.utron.bean.ItemProject;
import com.wolfalpha.utron.bean.UserBaseInfo;
import com.wolfalpha.utron.service.UserService;
import com.wolfalpha.utron.service.impl.UserServiceImpl;
import com.wolfalpha.utron.ui.UIHelper;
import com.wolfalpha.utron.utils.ThreadPoolUtils;
import com.wolfalpha.utron.view.XListView;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的关注
 * <p/>
 * Created by luodon on 15/8/19.
 */
public class MyAttentionActivity extends BaseNormalActivity implements XListView.IXListViewListener, View.OnClickListener {

    private XListView lvPartners;      //合伙人列表控件
    private XListView lvProjects;      //项目列表控件

    private LinearLayout llBarPartenr;   //操作栏
    private LinearLayout llBarProject;   //操作栏

    private TextView tvBarPartner;          //蓝色bar
    private TextView tvBarProject;          //蓝色bar

    private CheckBox cbPartner1;
    private CheckBox cbPartner2;

    private CheckBox cbProject1;
    private CheckBox cbProject2;

    private ViewFlipper vf;                 //翻页

    private ItemProjectAttentionedAdapter attentionedAdapter;//用户适配器
    private List<UserBaseInfo> userBaseInfos = new ArrayList<UserBaseInfo>();

    private ItemProjectAdapter projectAdapter;//项目适配器
    private List<ItemProject> projects = new ArrayList<ItemProject>();

    private int index = 0;          //标志当前位

    private UserService userService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_attention);

        userService = new UserServiceImpl();


        intiView();
        initListeners();
        loadData();
    }

    /**
     * 加载列表的数据
     */
    private void loadData() {
        attentionedAdapter = new ItemProjectAttentionedAdapter(this, userBaseInfos);
        projectAdapter = new ItemProjectAdapter(this, projects);

        lvPartners.setAdapter(attentionedAdapter);
        lvProjects.setAdapter(projectAdapter);

        ThreadPoolUtils.execute(new Runnable() {
            @Override
            public void run() {
                projects = userService.getMyProjectAttentions(1);
                runOnUiThread(new Runnable() {
                                  @Override
                                  public void run() {
                                      if (projects != null) {
                                          projectAdapter.setData(projects);
                                          if (projects.size() < AppConfig.LIST_PAGE_SIZE) {
                                              lvProjects.setPullLoadEnable(false);
                                          } else {
                                              lvProjects.setPullLoadEnable(true);
                                          }
                                      } else {
                                          projectAdapter.setData(new ArrayList<ItemProject>());
                                          lvProjects.setPullLoadEnable(false);
                                      }
                                  }
                              }

                );
                userBaseInfos = userService.getMyPartnerAttentions(1);

                runOnUiThread(new Runnable() {
                                  @Override
                                  public void run() {
                                      if (userBaseInfos != null) {
                                          attentionedAdapter.setData(userBaseInfos);
                                          if (userBaseInfos.size() < AppConfig.LIST_PAGE_SIZE) {
                                              lvPartners.setPullLoadEnable(false);
                                          } else {
                                              lvPartners.setPullLoadEnable(true);
                                          }
                                      } else {
                                          attentionedAdapter.setData(new ArrayList<UserBaseInfo>());
                                          lvPartners.setPullLoadEnable(false);
                                      }
                                  }
                              }

                );
            }
        });
    }

    /**
     * 初始化界面
     */

    private void intiView() {
        setTvTitle("我的关注");

        vf = (ViewFlipper) findViewById(R.id.vf_my_attention);

        llBarPartenr = (LinearLayout) findViewById(R.id.ly_attention_partner);
        llBarProject = (LinearLayout) findViewById(R.id.ly_attention_project);
        lvPartners = (XListView) findViewById(R.id.xl_my_attention_partenrs);
        lvProjects = (XListView) findViewById(R.id.xl_my_attention_projects);

        lvProjects.setXListViewListener(this);
        lvPartners.setXListViewListener(this);

        lvProjects.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position < 1 || position > projects.size()) {
                    //过滤刷新头部
                    return;
                } else {
                    Bundle bundle = new Bundle();
                    bundle.putInt("pId", projects.get(position - 1).getId());
                    UIHelper.showActivity(MyAttentionActivity.this, ProjectDetailActivity.class, bundle);
                }
            }
        });

        lvPartners.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position < 1 || position > userBaseInfos.size()) {
                    //过滤刷新头部
                    return;
                } else {
                    Bundle bundle = new Bundle();
                    bundle.putInt("userId", userBaseInfos.get(position - 1).getUser_id());
                    UIHelper.showActivity(MyAttentionActivity.this, UserInfoActivity.class, bundle);
                }
            }
        });

        tvBarPartner = (TextView) findViewById(R.id.tv_partner_partner_blue_bar_1);
        tvBarProject = (TextView) findViewById(R.id.tv_partner_partner_blue_bar_2);

        cbPartner1 = (CheckBox) findViewById(R.id.cb_partner_1);
        cbPartner2 = (CheckBox) findViewById(R.id.cb_partner_2);
        cbProject1 = (CheckBox) findViewById(R.id.cb_project_1);
        cbProject2 = (CheckBox) findViewById(R.id.cb_project_2);

        lvPartners.setPullLoadEnable(false);
        lvProjects.setPullLoadEnable(false);
    }

    /**
     * 初始化监听
     */
    private void initListeners() {

        llBarPartenr.setOnClickListener(this);
        llBarProject.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.ly_attention_partner:
                tvBarPartner.setVisibility(View.VISIBLE);
                tvBarProject.setVisibility(View.INVISIBLE);

                cbPartner1.setChecked(true);
                cbPartner2.setChecked(true);
                cbProject1.setChecked(false);
                cbProject2.setChecked(false);

                show(index, 0);
                index = 0;
                break;
            case R.id.ly_attention_project:
                tvBarProject.setVisibility(View.VISIBLE);
                tvBarPartner.setVisibility(View.INVISIBLE);

                cbPartner1.setChecked(false);
                cbPartner2.setChecked(false);
                cbProject1.setChecked(true);
                cbProject2.setChecked(true);

                show(index, 1);
                index = 1;
                break;
        }

    }

    /**
     * 翻页
     *
     * @param from
     * @param to
     */
    public void show(int from, int to) {
        if (from == to) {
            return;
        }
        if (from > to) {
            vf.showPrevious();
        }
        if (from < to) {
            vf.showNext();
        }

    }

    private int page1 = 1;
    private int page2 = 1;

    @Override
    public void onRefresh() {
        if (index == 0) {
            //合伙人
            page1 = 1;
            ThreadPoolUtils.execute(new Runnable() {
                @Override
                public void run() {
                    final List<UserBaseInfo> userBaseInfos1 = userService.getMyPartnerAttentions(page1);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (userBaseInfos1 != null) {
                                userBaseInfos = userBaseInfos1;
                                attentionedAdapter.setData(userBaseInfos);
                                if (userBaseInfos.size() < AppConfig.LIST_PAGE_SIZE) {
                                    lvPartners.setPullLoadEnable(false);
                                } else {
                                    lvPartners.setPullLoadEnable(true);
                                }
                            } else {
                                attentionedAdapter.setData(new ArrayList<UserBaseInfo>());
                                lvPartners.setPullLoadEnable(false);
                            }
                            lvPartners.stopRefresh();
                        }
                    });
                }
            });
        } else {
            //项目
            page2 = 1;
            ThreadPoolUtils.execute(new Runnable() {
                @Override
                public void run() {
                    final List<ItemProject> projects1 = userService.getMyProjectAttentions(page2);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (projects1 != null) {
                                projects = projects1;
                                projectAdapter.setData(projects);
                                if (projects1.size() < AppConfig.LIST_PAGE_SIZE) {
                                    lvProjects.setPullLoadEnable(false);
                                } else {
                                    lvProjects.setPullLoadEnable(true);
                                }
                            } else {
                                projectAdapter.setData(new ArrayList<ItemProject>());
                                lvProjects.setPullLoadEnable(false);
                            }
                            lvProjects.stopRefresh();
                        }
                    });
                }
            });
        }
    }

    @Override
    public void onLoadMore() {
        if (index == 0) {
            //合伙人
            ThreadPoolUtils.execute(new Runnable() {
                @Override
                public void run() {
                    final List<UserBaseInfo> userBaseInfos1 = userService.getMyPartnerAttentions(++page1);

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (userBaseInfos1 != null) {
                                userBaseInfos.addAll(userBaseInfos1);
                                attentionedAdapter.setData(userBaseInfos);
                                if (userBaseInfos.size() < AppConfig.LIST_PAGE_SIZE) {
                                    lvPartners.setPullLoadEnable(false);
                                } else {
                                    lvPartners.setPullLoadEnable(true);
                                }
                            } else {
                                page1--;
                                lvPartners.setPullLoadEnable(false);
                            }
                            lvPartners.stopLoadMore();
                        }
                    });

                }
            });
        } else {
            //项目
            ThreadPoolUtils.execute(new Runnable() {
                @Override
                public void run() {
                    final List<ItemProject> projects1 = userService.getMyProjectAttentions(++page2);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (projects1 != null) {
                                projects.addAll(projects1);
                                projectAdapter.setData(projects);
                                if (projects1.size() < AppConfig.LIST_PAGE_SIZE) {
                                    lvProjects.setPullLoadEnable(false);
                                } else {
                                    lvProjects.setPullLoadEnable(true);
                                }
                            } else {
                                page2--;
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        lvProjects.setPullLoadEnable(false);
                                    }
                                });
                            }
                            lvProjects.stopLoadMore();
                        }
                    });
                }
            });
        }
    }
}
